// pages/detail/detail.js
var timeFun = require('../../utils/util.js')
function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}
var commentData = new Array()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "objId":"",
    "userId":"",
    "detail":{},
    "screenWidth": '375px',
    "commentList": commentData,
    "pageNum": 0,
    "isLoadingMore":false,
    "showMask" :false,
    "isPlay": false,
    "focus": false,
    "time":''
  },
  inputValue: '',
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  videoBindPlay: function(e){
    this.setData({
      isPlay: true
    })
  },
  onCommentClick: function(event){
    this.setData({
      focus: true
    })
  },
  onFavClick: function(event){
    var articleId = event.currentTarget.dataset.articleid
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoUser/fav/save.html',
      data: {
        userId: wx.getStorageSync("userId"),
        articleId: articleId
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '收藏成功',
          icon: 'success',
          duration: 1000
        })
      }, fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log(res)
      }
    })
  },
  onShareClick: function(event){
    this.setData({
      showMask: true
    })
  },
  bindInputConfirm:function(e){
    var that = this
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoUser/comment/save.html?userId=' 
      + wx.getStorageSync('userId') + "&articleId=" + that.data.objId
      +"&content="+e.detail.value
      +"&parentId=''",
      method:'POST',
      data:{
        'userId': wx.getStorageSync("userId"),
        'articleId':that.data.objId,
        'content': e.detail.value,
        'parentId':''
      },
      success:function(res){
        if(res.data.code=="0"){
        wx.showToast({
          title: '评论成功',
          icon:"success"
        })}
      },fail:function(e){
        console.log(e)
      }
    })
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "screenWidth": res.windowWidth + 'px'
        })
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
      }
    })
    this.setData({
      "objId":options.objId,
      "userId":options.userId,
      "showMask": options.showMask
    })
    var that = this
    if (wx.canIUse("wx.showLoading")) {
      wx.showLoading({
        title: 'Loading',
      })
    } else {
      wx.showNavigationBarLoading()
    }
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoArticle/detail.html',
      method:"GET",
      data:{
        "objId":that.data.objId,
        "userId":that.data.userId
      },
      success:function(res){
        var data = res.data.data
        var tags = data.tags
        var tagArray = tags.split(',')
        data.tags = tagArray
        that.setData({
          "detail":data,
          "time": timeFun.formatTimeToHuman(res.data.data.createTime)
        })
      },
      complete:function(){
        if (wx.canIUse("wx.hideLoading")){
          wx.hideLoading()
        } else {
          wx.hideNavigationBarLoading()
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getComment()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("end")
    this.getComment()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    return {
      title: that.data.intro,
      path: '/pages/detail/detail?objId='+that.data.detail.id,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  onKnownClick: function(){
    this.setData({
      showMask:false
    })
  },
  getComment:function(){
    var that = this
    if (this.data.isLoadingMore) {
      return
    }
    this.setData({
      'isLoadingMore': true
    })
    wx.showNavigationBarLoading()
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoUser/comment/list.html',
      method: 'GET',
      data: {
        'articleId': that.data.objId,
        'pageNum': that.data.pageNum,
        'pageSize': 20,
        'pageSizeHot': 0
      },
      success: function (e) {
        wx.hideNavigationBarLoading()
        if (e.data.code == "0") {
          if (e.data.data.viewList.length > 0) {
            var list = commentData.concat(e.data.data.viewList)
            that.setData({
              'commentList': list,
              'pageNum': that.data.pageNum + 1,
              'isLoadingMore': false
            })
          } else {
            that.setData({
              'isLoadingMore': false
            })
          }
        }
      },
      fail: function (e) {
        wx.hideNavigationBarLoading()
        that.setData({
          'isLoadingMore': false
        })
      }
    })
  }
  
})